﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using ElectronStoreDAL;

namespace BackEnd
{
	public partial class Catalog : WebManagement.UserAuthentificatedPage
	{
		private ElectronStoreSource dataSource;
		private List<ElectronStoreDAL.Category> listCategories;
		private List<ElectronStoreDAL.Product> listProduct;
		private int currentCategory;

		public Catalog() : base(GeneralResource.urlLogin, GeneralResource.nameCookie){}

		protected void Page_Load(object sender, EventArgs e)
		{
			dataSource = new ElectronStoreSource();
			listCategories = dataSource.GetCategories();

			if (listCategories == null)
				return;

			if (!int.TryParse(Request.Params["id"], out currentCategory))
				currentCategory = 1;
			listProduct = dataSource.ProductsFromCategory(currentCategory);
		}


		protected void Page_PreRender(object sender, EventArgs e)
		{
				if (listCategories != null)
					rpCategory.DataSource = listCategories;
				else
					return;

				if (listProduct != null)
					rpProduct.DataSource = listProduct;

				Page.DataBind();
		}


		#region Category

		protected void rpCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				ElectronStoreDAL.Category category = e.Item.DataItem as ElectronStoreDAL.Category;

				if (category != null)
				{
					if (category.CategoryID == currentCategory)
					{
						Label lblCat = e.Item.FindControl("lblCatName") as Label;
						if (lblCat != null)
							lblCat.Text = category.Name;
					}
					else
					{
						HyperLink linkCat = e.Item.FindControl("hlCatName") as HyperLink;
						if (linkCat != null)
						{
							linkCat.Text = category.Name;
							linkCat.NavigateUrl = string.Format(GeneralResource.urlCatalog, category.CategoryID);
						}
					}

					HyperLink linkEdit = e.Item.FindControl("hlEditCategory") as HyperLink;
					if (linkEdit != null)
						linkEdit.NavigateUrl = string.Format(GeneralResource.urlCategory, category.CategoryID);

					ImageButton btnDel = e.Item.FindControl("btnDelCategory") as ImageButton;
					if (btnDel != null)
						btnDel.CommandArgument = category.CategoryID.ToString();
				}
			}

		}	//protected void rpCategory_ItemDataBound


		protected void btnDelCategory_Click(object sender, EventArgs e)
		{
			ImageButton btn = sender as ImageButton;
			if (btn == null)
				return;

			int idCat;
			if (int.TryParse(btn.CommandArgument, out idCat))
			{
				dataSource.RemoveCategory(idCat);
				Response.Redirect(string.Format(GeneralResource.urlCatalog, listCategories[0].CategoryID));
			}
		}


		protected void btnAddCategory_Click(object sender, EventArgs e)
		{
			Response.Redirect(string.Format(GeneralResource.urlCategory, 0));
		}

		#endregion


		#region Product

		protected void rpProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				ElectronStoreDAL.Product product = e.Item.DataItem as ElectronStoreDAL.Product;

				if (product != null)
				{
					Label lblName = e.Item.FindControl("lblProdName") as Label;
					if (lblName != null)
						lblName.Text = product.Name;

					HyperLink linkEdit = e.Item.FindControl("hlEditProduct") as HyperLink;
					if (linkEdit != null)
						linkEdit.NavigateUrl = string.Format(GeneralResource.urlProduct, product.ProductID, currentCategory);

					ImageButton btnDel = e.Item.FindControl("btnDelProduct") as ImageButton;
					if (btnDel != null)
						btnDel.CommandArgument = product.ProductID.ToString();
				}
			}
		}


		protected void btnDelProduct_Click(object sender, EventArgs e)
		{
			ImageButton btn = sender as ImageButton;
			if (btn == null)
				return;

			int id;
			if (int.TryParse(btn.CommandArgument, out id))
			{
				dataSource.RemoveProductFromCategory(id, currentCategory);
				listProduct = dataSource.ProductsFromCategory(currentCategory);
			}

		}


		protected void btnAddProduct_Click(object sender, EventArgs e)
		{
			Response.Redirect(string.Format(GeneralResource.urlProduct, 0, currentCategory));
		}

		#endregion

	}	//public partial class Catalog : Technical.UserAutentification

}	//namespace BackEnd